<?php
require_once 'BaseMySQLDAO.php';

class AppLinkDAO extends BaseMySQLDAO {

    public function __construct($context) {
        parent::__construct($context, 'app_link');
    }

	public function getAllAppLink($start, $limit) {
		
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "	SELECT
							app_link.type as type,
							app_link.file_type as file_type,
							app_link.id as id,
							app_link.app_id as app_id,
							app_link.device as device,
							device_id,
							app_link.link as link,
							app_link.app_id as app_id,
							app.`name` as app_name,
							IF (ISNULL(device.device_name),'0',device.device_name) as device_name
						FROM
							app_link
						LEFT JOIN app ON app_link.app_id = app.id
						LEFT JOIN device ON app_link.device_id = device.id
						ORDER BY app_link.id DESC
            			LIMIT :start, :limit 
            		 ";
            $param = array(":start" => $start, ":limit" => $limit);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadManyRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }
    
	public function countAppLink() {
        $result = null;
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "  SELECT count(id) as total FROM app_link ";
            
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadOneRow($inSQL);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result['total'];
    }
    
	public function getAppLinkByID($id) {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "SELECT * FROM app_link WHERE id = :id";
            $param = array(':id' => $id);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadOneRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }
    
    public function getAppLinkByAppId($id) {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "SELECT
							app_link.type as type,
							app_link.file_type as file_type,
							app_link.id as id,
							app_link.app_id as app_id,
							app_link.device as device,
							device_id,
							app_link.link as link,
							app_link.app_id as app_id,
							app.`name` as app_name,
							IF (ISNULL(device.device_name),'0',device.device_name) as device_name
						FROM
							app_link
						LEFT JOIN app ON app_link.app_id = app.id
						LEFT JOIN device ON app_link.device_id = device.id
						WHERE app_id = :app_id 
						ORDER BY app_link.id DESC
						";
            $param = array('app_id' => $id);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadManyRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }
    
	public function insertAppLink($param) {
        $result = null;
        try {
            $inSQL = "INSERT INTO
                        app_link(id, app_id, type, file_type, device, device_id, link)
                        VALUES (
                            :id, :app_id, :type, :file_type, :device, :device_id, :link
                        )";
            $queryParam = array(
                                ':id' => NULL,
                                ':app_id' => $param['app_id'],
                                ':type' => $param['type'],
                                ':file_type' => $param['file_type'],
                                ':device' => $param['device'],
                                ':device_id' => $param['device_id'],
            					':link' => $param['link']
                            );
            $this -> getContext() -> getTransaction() -> startTransaction('thanhphogame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('mobile');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('choigamemobi');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('mymobigame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('ngoisaogamethu');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

        } catch (Exception $exception) {
            print_r($exception);
            exit;
        }
        return $result;
    }
    
	public function updateAppLink($param) {
        $result = null;
        try {
            $inSQL = "UPDATE app_link SET
                                    app_id = :app_id,
                                    type = :type,
                                    file_type = :file_type,
                                    device = :device,
                                    device_id = :device_id,
                                    link = :link
                        WHERE id = :id
                        ";
            $queryParam = array(
                                ':id' => $param['id'],
                                ':app_id' => $param['app_id'],
                                ':type' => $param['type'],
                                ':file_type' => $param['file_type'],
                                ':device' => $param['device'],
                                ':device_id' => $param['device_id'],
            					':link' => $param['link']
                            );
            $this -> getContext() -> getTransaction() -> startTransaction('thanhphogame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('mobile');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('choigamemobi');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('mymobigame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('ngoisaogamethu');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
         

        } catch (Exception $exception) {
            print_r($exception);
            exit;
        }
        return $result;
    }
    
	public function deleteAppLink($id) {
        try {

            $inSQL = "   DELETE FROM app_link WHERE id = :id ";
            $queryParam = array(':id' => $id);
            $this -> getContext() -> getTransaction() -> startTransaction('thanhphogame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('mobile');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('choigamemobi');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('mymobigame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('ngoisaogamethu');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
            return false;
        }
        return true;
    }

}
?>